package com.qingneng.web;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
import com.qingneng.service.sdk.ICustomerInfoService;
import com.qingneng.vo.CustomerInfo;
import com.tbfs.common.BaseAction;

public class SaveCustomerInfoAction extends BaseAction implements
		ModelDriven<CustomerInfo> {

	private static final Logger logger = Logger
			.getLogger(SaveCustomerInfoAction.class);

	private CustomerInfo customerInfo = new CustomerInfo();

	public CustomerInfo getCustomerInfo() {
		return customerInfo;
	}

	public void setCustomerInfo(CustomerInfo customerInfo) {
		this.customerInfo = customerInfo;
	}

	private String message = null;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	// init service
	private ICustomerInfoService customerInfoService;

	// 构造函数
	public SaveCustomerInfoAction(ICustomerInfoService customerInfoService) {
		this.customerInfoService = customerInfoService;
	}

	public SaveCustomerInfoAction() {

	}

	/**
	 * save role data
	 * 
	 * @return
	 */
	public String saveData() {
		if (logger.isDebugEnabled()) {
			logger.debug("+++begin save role +++");
		}
		logger.info("customerInfo_name:" + customerInfo.getCustomerName());

		try {

			int id = customerInfo.getId();
			// 修改
			if (id > 0) {
				customerInfoService.modify(customerInfo);
			} else {
				customerInfoService.add(customerInfo);
			}
			customerInfo.setMessage("success");
		} catch (Exception e) {
			customerInfo.setMessage("添加客户失败");
			e.printStackTrace();
		}
		if (logger.isDebugEnabled()) {
			logger.debug("+++end save role +++");
		}
		return Action.SUCCESS;
	}

	public String destoryData() {
		if (logger.isDebugEnabled()) {
			logger.debug("+++begin delete role +++");
		}
		try {
			customerInfoService.destory(customerInfo);
			customerInfo.setMessage("success");
		} catch (Exception e) {
			customerInfo.setMessage("删除客户失败");
			e.printStackTrace();
		}

		if (logger.isDebugEnabled()) {
			logger.debug("+++end delete role +++");
		}
		return Action.SUCCESS;
	}

	public CustomerInfo getModel() {
		// TODO Auto-generated method stub
		return customerInfo;
	}

}
